<?php
	/*POST-add new or update*/
	@$pname = $_POST['pname'];
	@$pid = $_POST['pid'];
	
	@$cname = $_POST['cname'];
	@$idcatparent = $_POST['idcatparent'];
	@$cid = $_POST['cid'];
	/*parent*/
	if(isset($pname))
	{
		if($pid == '')/*add new*/
		{
			$esc_val = $exec_mysql->escape_string(array($pname));
			$query = sprintf("insert into category(name) values('%s')", $esc_val[0]);
			$exec_mysql->execute($query, 0);
		}
		else/*edit*/
		{
			$esc_val = $exec_mysql->escape_string(array($pname, $pid));
			$query = sprintf("update category set name = '%s' where id = %s", $esc_val[0], $esc_val[1]);
			$exec_mysql->execute($query, 0);
		}
	}
	
	/*child*/
	if(isset($cname) && isset($idcatparent))
	{
		if($cid == '')/*add new*/
		{
			$esc_val = $exec_mysql->escape_string(array($cname));
			$query = sprintf("insert into sub_category(id_parent, name) values($idcatparent, '%s')", $esc_val[0]);
			$exec_mysql->execute($query, 0);
		}
		else/*edit*/
		{
			$esc_val = $exec_mysql->escape_string(array($cname));
			$query = sprintf("update sub_category set id_parent = $idcatparent, name = '%s' where id = $cid", $esc_val[0]);
			$exec_mysql->execute($query, 0);
		}
	}
	
	/*GET-edit or delete parent*/
	@$pid = $_GET['pid'];
	@$pact = $_GET['pact'];
	$pnam_to_edit = '';
	$p_button_text = 'Thêm';
	if($pid != '')
	{
		$esc_val = $exec_mysql->escape_string(array($pid));
		if($pact == 'edit')
		{
			$p_button_text = 'Cập nhật';
			$query = sprintf("select name from category where id = '%s'", $esc_val[0]);
			$pnam_to_edit = $exec_mysql->execute($query, 1);
		}
		else if($pact == 'del')
		{
			$query = sprintf("delete from category where id = '%s'", $esc_val[0]);
			$exec_mysql->execute($query, 0);
		}
		else
		{
		}
	}
	
	/*GET-edit or delete child*/
	@$cid = $_GET['cid'];
	@$cact = $_GET['cact'];
	$cnam_to_edit = '';
	$c_pid = -1;
	$c_button_text = 'Thêm';
	if($cid != '')
	{
		$esc_val = $exec_mysql->escape_string(array($cid));
		if($cact == 'edit')
		{
			$c_button_text = 'Cập nhật';
			$query = sprintf("select id_parent, name from sub_category where id = '%s'", $esc_val[0]);
			$result = $exec_mysql->execute($query, 0);
			while($row = mysql_fetch_array($result))
			{
				$cnam_to_edit = $row['name'];
				$c_pid = $row['id_parent'];
			}
		}
		else if($cact == 'del')
		{
			$query = sprintf("delete from sub_category where id = '%s'", $esc_val[0]);
			$exec_mysql->execute($query, 0);
		}
		else
		{
		}
	}
?>
<script type="text/javascript">
	function checkParentSubmit(){
		if(document.parent.pname.value == ''){
			showAlert('Thông báo', 'Vui lòng nhập tên danh mục', 'pname');
			return false;
		}
		return true;
	}
	
	function checkChildSubmit(){
		if(document.child.cname.value == ''){
			showAlert('Thông báo', 'Vui lòng nhập tên danh mục', 'cname');
			return false;
		}
		return true;
	}
	
	function delCat(url){
		window.location = url;
	}
</script>
<p class="group-line">
    <span class="group-title">Danh mục cha</span>
</p>
<form method="post" name="parent" action="admin_master_page.php?cmd=3.3" onsubmit="return checkParentSubmit()">
	 <input type="hidden" name="pid" value="<?php echo htmlspecialchars($pid);?>" />
     <table class="form-layout">
    	<tr>
        	<td>Tên danh mục cha</td>
        	<td><input type="text" name="pname" id="pname" style="width:400px" value="<?php echo htmlspecialchars($pnam_to_edit);?>" /></td>
        	<td><input type="submit" value="<?php echo $p_button_text; ?>" /></td>
    	</tr>
     </table>
</form>

<table class="table-list" width="90%">
	<tr class="table-header"><td>ID</td><td>Tên danh mục</td><td>Sửa</td><td>Xóa</td></tr>
<?php
	$result = $exec_mysql->execute("select * from category", 0);
	$cat = array();
	while($row = mysql_fetch_array($result))
	{
		$cat[$row['id']] = $row['name'];
		echo '<tr>';
		echo '<td align="center">'.$row['id'].'</td>';
		echo '<td>'.$row['name'].'</td>';
		echo '<td align="center"><a href="admin_master_page.php?cmd=3.3&pid='.$row['id'].'&pact=edit"><img src="img/ico_edit.png" title="Sửa" /></a></td>';
		echo '<td align="center"><a href="javascript:void()"><img src="img/ico_delete.png" title="Xóa" onclick="showConfirm(\'Xác nhận\', \'Bạn có chắc muốn xóa danh mục này không?\', \'delCat([admin_master_page.php?cmd=3.3&pid='.$row['id'].'&pact=del])\')" /></a></td>';
		echo '</tr>';
	}
?>
</table>
<p class="group-line">
    <span class="group-title">Danh mục con</span>
</p>
<form method="post" name="child" action="admin_master_page.php?cmd=3.3" onsubmit="return checkChildSubmit()">
	 <input type="hidden" name="cid" value="<?php echo htmlspecialchars($cid);?>" />
     <table class="form-layout">
     	<tr>
        	<td>Danh mục cha</td>
            <td>
            	<select name="idcatparent">
                <?php
                	foreach($cat as $id => $name)
					{
						if($id == $c_pid)
							echo '<option value="'.$id.'" selected="selected">'.$name.'</option>';
						else
							echo '<option value="'.$id.'">'.$name.'</option>';
					}
				?>
                </select>
            </td>
            <td></td>
        </tr>
        <tr>
        	<td>Tên danh mục con</td>
            <td><input type="text" name="cname" id="cname" value="<?php echo $cnam_to_edit; ?>" style="width:400px" /></td>
            <td><input type="submit" value="<?php echo $c_button_text; ?>" /></td>
        </tr>
     </table>
</form>
<table class="table-list" width="90%">
	<tr class="table-header"><td>ID</td><td>Tên danh mục cha</td><td>Tên danh mục</td><td>Sửa</td><td>Xóa</td></tr>
<?php
	$result = $exec_mysql->execute("select * from sub_category order by id_parent asc", 0);
	while($row = mysql_fetch_array($result))
	{
		$parent = $exec_mysql->execute("select name from category where id = ".$row['id_parent'], 1);
		echo '<tr>';
		echo '<td align="center">'.$row['id'].'</td>';
		echo '<td>'.$parent.'</td>';
		echo '<td>'.$row['name'].'</td>';
		echo '<td align="center"><a href="admin_master_page.php?cmd=3.3&cid='.$row['id'].'&cact=edit"><img src="img/ico_edit.png" title="Sửa" /></a></td>';
		echo '<td align="center"><a href="javascript:void()"><img src="img/ico_delete.png" title="Xóa" onclick="showConfirm(\'Xác nhận\', \'Bạn có chắc muốn xóa danh mục này không?\', \'delCat([admin_master_page.php?cmd=3.3&cid='.$row['id'].'&cact=del])\')" /></a></td>';
		echo '</tr>';
	}
?>
</table>